import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vichat.business.entity.DistrictFence;
import com.vichat.business.entity.DistrictFenceRule;
import com.vichat.business.entity.DistrictFenceUser;
import com.vichat.business.vo.FenceVO;
import com.vichat.common.cache.RedisHelper;
import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.util.*;
import com.vichat.gps.entity.LocationData;
import com.vichat.res.vo.RecFileVO;
import com.vichat.room.vo.RoomVO;
import com.vichat.user.vo.RequestParamsVO;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * Created by user on 2017/12/10.
 */
public class apiHttpPost {
    private static Log logger = LogFactory.getLog(apiHttpPost.class);
    //private static String defaultUrl = "http://117.71.39.75:6100/gtalkApi/";
    private static String defaultUrl = "http://47.100.112.4:6100/userApi/";
    //private static String defaultUrl = "http://localhost:8888/userApi/";
    public static void main(String[] args) {
        //申请企业KEY
//        addRoomMember();
        applyPartyKey();
//        delRoomMember();
//        System.out.println(RedisHelper.getFenceAlarmTime(1l,1l)==null?"":RedisHelper.getFenceAlarmTime(1l,1l));

//        String alarmMsg = I18NUtil.getMessage("GpsApiController.uploadGps.msg_sendAlarmIn", new Object[]{"123", "123",new Date(1550457539958l)}, GlobalConstant.SYS_LANGUAGE);
//        System.out.println(alarmMsg);

    }

    /**
     * 外部接口新增用户
     */
    public static void applyPartyKey(){
//        String url = "http://localhost:8080/vichat_web_war/vcapi/fence/queryAlarm";
//        String url = "http://localhost:8080/vichat_web_war/vcapi/room/createRoom";
//        String url = "http://localhost:8080/vichat_web_war/vcapi/saveSystemMessage?msgIds=1&msgIds=2";
        String url = "http://localhost:8080/vichat_web_war/gpsApi/uploadGps";
//        String url = "http://localhost:8080/vichat_web_war/vcapi/fence/saveFenceUser";

//        String url ="http://localhost:8080/vichat_web_war/vcapi/fence/querySystemMessage%20?page=1&rows=10&startDate=&endDate=&objId=64&objType=2";
        HashMap map = new HashMap();
        map.put("packageName","com.hytera.chat");
        List<LocationData> data = new ArrayList<>();
        data.add(getLocationData());
        /**
         * adminGrade: 5
         * priority: 2
         * rid: 100110155
         * timelen: 120
         * uids: [300003324, 300003325, 300003326, 300003327]
         * 0: 300003324
         * 1: 300003325
         * 2: 300003326
         * 3: 300003327
         */

        RoomVO vo = new RoomVO();


        System.out.println(post(url,JSONObject.toJSONString(data)));
//        System.out.println(post(url,""));
    }

    public static LocationData getLocationData(){
        LocationData data = new LocationData();
        data.setUid(300000019l);
        data.setLatitude("31.156046");
        data.setLongitude("121.564389");
        data.setAddress("上海市浦东新区沪南路2337号靠近复地万科活力城A座");
        data.setSpeed("0");
        data.setLocType(5);
        data.setLocTime(1551417386713L);
        data.setSumtime(0l);
        data.setPartyId(1l);
        data.setOrgCode("000001");
        data.setOrgId(1l);
        return data;
    }
    /**
     * 外部接口新增用户
     */
    public static void addRoomMember(){
        String url = "http://localhost:8080/roomApi/addRoomMember";
        HashMap map = new HashMap();
        map.put("uid",300000011L);
        map.put("rid",7l);

        List<Long> uids = new ArrayList<>();
        uids.add(300000001L);
        uids.add(300000002L);
        uids.add(300000003L);
        uids.add(300000004L);
        uids.add(300000005L);

        map.put("uids",uids);

        System.out.println(post(url,JSON.toJSONString(map)));
    }
    /**
     * 外部接口新增用户
     */
    public static void delRoomMember(){
        String url = "http://localhost:8080/roomApi/delRoomMember";
        HashMap map = new HashMap();
        map.put("uid",300000011L);
        map.put("rid",7l);

        List<Long> uids = new ArrayList<>();
        uids.add(300000001L);
        uids.add(300000002L);
        uids.add(300000003L);
        uids.add(300000004L);
        uids.add(300000005L);

        map.put("uids",uids);

        System.out.println(post(url,JSON.toJSONString(map)));
    }

    /**
     * post 方法
     * @param url
     * @param json
     * @return
     */
    private static String post(String url, String json) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        String responseBody = null;
        try {
            // 建立一个NameValuePair数组，用于存储欲传送的参数
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            long startTime = System.currentTimeMillis();
            HttpResponse response = httpClient.execute(method);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            logger.info("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed:" + response.getStatusLine());
            }
            // Read the response body
            responseBody = EntityUtils.toString(response.getEntity());
            logger.info("输出参数：" + responseBody);
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        } finally {
            return responseBody;
        }
    }
}
